﻿using JustMetClient.Commons;
using JustMetClient.Models.InfoModels;
using JustMetClient.Models.StrategyModels;
using JustMetClient.Models.UserModels;
using JustMetClient.Models.WebApiModels;
using JustMetClient.Modules.WebModule;
using Azylee.Core.DataUtils.CollectionUtils;
using Azylee.Core.DataUtils.EncryptUtils;
using Azylee.Core.DataUtils.StringUtils;
using Azylee.Core.IOUtils.TxtUtils;
using Azylee.YeahWeb.HttpUtils;
using System;
using System.Collections.Generic;

namespace JustMetClient.Modules.LoginModule
{
    public static class LoginHelper
    {
        /// <summary>
        /// 根据计算机信息自动获取用户账号和密码
        /// </summary>
        /// <param name="account"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public static bool GetAccountAndPassword(out string account, out string password)
        {
            account = "";
            password = "";
            if (Str.Ok(R.Account) && R.Account.StartsWith("0105"))
            {
                //工号登录
                account = R.Account;
                password = R.Account;
                return true;
            }
            else
            {
                //MAC登录
                if (Str.Ok(R.NetInfo.Mac))
                {
                    account = R.NetInfo.Mac;
                    password = R.NetInfo.Mac + MD5Tool.Encrypt(R.NetInfo.Mac);
                    return true;
                }
            }
            return false;
        }
        /// <summary>
        /// 认证检查用户信息
        /// </summary>
        /// <param name="acc"></param>
        /// <param name="pwd"></param>
        /// <param name="result"></param>
        /// <param name="user"></param>
        /// <returns></returns>
        public static bool CheckUserInfo(string acc, string pwd, out WebResultModel result, out UserModel user)
        {
            result = null;
            user = null;
            if (Str.Ok(acc) && Str.Ok(pwd))
            {
                string fullUrl = R.Web.Get.Login + $"?account={acc}&password={pwd}";//拼接地址
                result = HttpTool.Get<WebResultModel>(fullUrl);//请求数据
                if (result != null && result.OK)
                {
                    user = new UserModel();//组装用户模型
                    user.Account = acc;
                    if (result.Data.TryGetValue("Name", out string name)) user.Name = name;
                    if (result.Data.TryGetValue("StrategyGroup", out string group)) user.StrategyGroup = group;
                    if (result.Data.TryGetValue("Dept", out string dept)) user.Dept = dept ?? "";
                    if (result.Data.TryGetValue("DeptNo", out string deptno)) user.DeptNo = deptno ?? "";

                    //缓存登录的相关信息
                    R.Account = user.Account;
                    R.UserName = user.Name;
                    R.StrategyGroup = user.StrategyGroup;
                    R.Dept = user.Dept;
                    R.DeptNo = user.DeptNo;

                    //认证成功发送认证成功信息
                    WebApiSendQueue.Add(R.Web.Send.ClientCollection, new ClientUserDefinedInfoModel()
                    {
                        Type = "认证用户信息",
                        Values = ArrayTool.Formatter(new string[] { R.Account, R.UserName, R.StrategyGroup }, "", 10),
                    }, HttpMethodType.Post, 10);

                    return true;
                }
            }
            return false;
        }

        /// <summary>
        /// 自动执行登录功能
        /// </summary>
        /// <returns></returns> 
        public static bool AutoLogin()
        {
            if (GetAccountAndPassword(out string account, out string password))
            {
                //1、认证用户信息
                bool userAuth = CheckUserInfo(account, password, out WebResultModel result, out UserModel user);
                if (userAuth)
                {
                    R.Flag.IsAuthOk = true;
                    R.Account = account;
                    R.Password = password;
                    R.UserName = user.Name;
                    R.StrategyGroup = user.StrategyGroup;

                    //存储认证账号和姓名
                    IniTool.Set(R.Files.Settings, "User", "LastName", R.UserName);
                    IniTool.Set(R.Files.Settings, "User", "LastAccount", R.Account);
                }
            }
            return false;
        }
    }
}
